<template>
  <div class="app-container">
    <div class="contentBox">
      <div class="searchBox">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
          <el-form-item prop="locationId" style="margin: 0 6px 0 0;">
            <SelectOption class="selectOptionBox" ref="locationSelectOption" selectType="multi" placeHolder="请选择区域" title="区域选择" maxSelectLength="more"
              v-model="queryParams.locationId" @loadOptions="locationDataLoad" @chooseConfirm="handleQuery()" @clearChoose="handleQuery()" @chooseError="handleQueryError"/>
          </el-form-item>
          <el-select v-model="queryFormName" style="width: 120px" @change="changeQuerySet">
            <el-option
              v-for="item in queryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-form-item :prop="queryFormName" style="margin: 0 6px 0 0;">
            <el-input
                style="width: 200px;"
                v-model="queryParams[queryFormName]"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery()"
            />
          </el-form-item>
          <el-form-item style="margin: 0 6px 0 0;">
            <el-button class="normalFormBg searchBtn" icon="Search" @click="handleQuery()"></el-button>
            <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetQuery()"></el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-row :gutter="10" style="margin: 16px 0;">
        <el-col :span="1.5">
          <div class="normalBtn pageFlex pageFlexCenter" v-hasPermi="['gasManage:archivesManage:gasMeter:add']" @click="handleAdd()" >
            <img src="@/assets/images/plus.png" class="normalBtn_icon">
            <div class="normalBtn_text">新增</div>
          </div>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="userNo" label="用户编号" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
        <el-table-column prop="userNo" label="用户名称" width="100"></el-table-column>
        <el-table-column prop="phoneNumber" label="手机号" width="100"></el-table-column>
        <el-table-column prop="address" label="所属区域" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="门牌号" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="通信地址" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="表类型" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="表地址" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="表状态" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="集中器名称" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="缴费类型" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="价格名称" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="卡类型" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="卡号" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="卡区域" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="证件类型" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="证件号码" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="固定电话" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="建筑面积" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="所属采集器" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="表型号" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="表底数" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="安装日期" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="塑封信息" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="用户余额" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="进气方向" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" align="center" width="340" class-name="small-padding fixed-width" fixed="right">
          <template #default="scope">
            <div class="pageFlex pageFlexCenter btnSetBox" @click="handleInfo(scope.row.id)">
              <img src="@/assets/images/fileManage.png" class="img">
              <div class="text">详情</div>
            </div>
            <div class="pageFlex pageFlexCenter btnSetBox" @click="handleEdit(scope.row.id)">
              <img src="@/assets/images/edit.png" class="img">
              <div class="text">修改</div>
            </div>
            <div class="pageFlex pageFlexCenter btnSetBox" @click="handleDelete(scope.row)">
              <img src="@/assets/images/delete.png" class="img">
              <div class="text">删除</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
        <div class="totalText">共计 {{total}} 条</div>
        <el-pagination background layout="prev, pager, next" :total="total" :current-page="queryParams.page" :page-size="queryParams.pageSize" @current-change="handleCurrentChange"/>
      </div>
    </div>
    <el-drawer v-model="modalShow" direction="rtl" size="600" :title="modalTitle">
      <div class="modalBox">
        <div class="modalBoxContent">
          <el-form
            ref="formRef"
            class="formContent"
            label-width="110"
            :model="form"
            :rules="rules"
            :disabled="modalTitle === '用户档案信息详情'"
          >
            <div class="formContentTop pageFlex pageAlignCenter">
              <div class="line"></div>
              <div class="title">用户档案信息</div>
            </div>
            <div class="formContentCenter bgFF">
              <el-form-item label="所属区域"></el-form-item>
              <el-form-item label="绑定燃气用户"></el-form-item>
              <el-form-item label="绑定燃气表"></el-form-item>
              <el-form-item label="价格配置"></el-form-item>
              <el-form-item label="详情地址"></el-form-item>
              <el-form-item label="门牌号"></el-form-item>
              <el-form-item label="楼层"></el-form-item>
              <el-form-item label="建筑面积"></el-form-item>
              <el-form-item label="人口"></el-form-item>
              <el-form-item label="户向"></el-form-item>
              <el-form-item label="缴费类型"></el-form-item>
              <el-form-item label="安装日期"></el-form-item>
              <el-form-item label="燃气表所在经度"></el-form-item>
              <el-form-item label="燃气表所在纬度"></el-form-item>
              <el-form-item label="代扣账号"></el-form-item>
              <el-form-item label="档案状态"></el-form-item>
              <el-form-item label="档案删除原因"></el-form-item>
              <el-form-item label="备注"></el-form-item>
            </div>
          </el-form>
        </div>
        <div class="lineBox" v-if="modalTitle !== '用户档案信息详情'"></div>
        <div class="bottomBox pageFlex pageFlexCenter" v-if="modalTitle !== '用户档案信息详情'">
          <el-button type="primary" :loading="submitLoading" @click="submitNow">确认提交</el-button>
        </div>
        <el-button type="primary" :loading="formLoading" class="modalLoadingBox" v-if="formLoading">加载数据中</el-button>
      </div>
    </el-drawer>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import { ElLoading, ElMessageBox } from 'element-plus';
import { searchGasMeterApi } from '@/api/gasManage/archivesManage/gasMeter'
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict(['sys_normal_disable']);
// ---------------------------------选择区域----------------------------------
import SelectOption from '@/views/gasManage/components/SelectOption';
import { areasList } from '@/api/system/location';
function handleQueryError(e){
  proxy.$modal.msgError(e.msg)
}
async function locationDataLoad(e){
  const { node, resolve } = e;
  let id = node.level === 0 ? 0 : node.data.id;
  let data = [];
  if(node.data.isEnable != 0){
    data = await areasListApi({parentId: id});
    if(data.length){
      data.map(item=>{
        item.checked = item.isEnable == 0;
        return item
      })
    }
  }
  proxy.$refs.locationSelectOption.handleLoadApi(data, node, resolve);
}
function areasListApi(params){
  return new Promise(resolve => {
    areasList(params).then(res => {
      resolve(res.data);
    }).catch(() => {resolve(false)})
  })
}
// -------------------------------跳转页面-------------------------------------
import { useRouter } from "vue-router";
const router = useRouter();
function toPage(link, row){
  let url = link;
  if(row){
    url += row.id
  }
  router.push(url);
}
// -------------------------------查询----------------------------------------
onMounted(()=>{
  handleQuery();
})
const queryFormName = ref('name');
const preFormName = ref('name');
const queryOptions = ref([
  {
    value: 'userNo',
    label: '用户编号',
  },
  {
    value: 'name',
    label: '用户名称',
  },
  {
    value: 'doorNum',
    label: '门牌号',
  },
  {
    value: 'phone',
    label: '手机号码',
  },
  {
    value: 'address',
    label: '通信地址',
  },
  {
    value: 'meterAddress',
    label: '表地址',
  },
  {
    value: 'focusName',
    label: '集中器名称',
  },
  {
    value: 'focusAddress',
    label: '集中器地址',
  },
  {
    value: 'userName',
    label: '抄表员名称',
  },
  {
    value: 'cardNum',
    label: '卡号',
  },
])
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  locationId: ''
})
const loading = ref(false);
const total = ref(0);
const dataList = ref([]);
function changeQuerySet(e){
  if(queryFormName.value !== preFormName.value){
    delete queryParams.value[preFormName.value];
    preFormName.value = e;
  }
}
function handleQuery(){
  queryParams.value.pageNum = 1;
  getList();
}
function resetQuery(){
  proxy.resetForm("queryRef");
  queryParams.value.locationId = '';
  proxy.$refs.locationSelectOption.setDefaultData();
  handleQuery();
}
async function getList(){
  loading.value = true;
  let data = await getGasMeterList(queryParams.value);
  if(data && Array.isArray(data)){
    dataList.value = data;
  }else{
    dataList.value = [];
  }
  loading.value = false;
}
function getGasMeterList(params){
  return new Promise(resolve => {
    searchGasMeterApi(params).then(res => {
      total.value = Number(res.total);
      resolve(Array.isArray(res.rows) ? res.rows : []);
    }).catch(() => {resolve(false)})
  })
}
function handleCurrentChange(e){
  queryParams.value.pageNum = e;
  getList();
}
// -------------------------------弹窗-------------------------------
const modalShow = ref(false);
const modalTitle = ref('');
const formLoading = ref(false);
const submitLoading = ref(false);
const form = ref({});
const rules = ref({});
function handleModalShow(name){
  modalTitle.value = name;
  modalShow.value = true;
}
function handleModalHide(){
  modalShow.value = false;
}
function submitNow(){

}
// ------------------------------详情--------------------------------
function handleInfo(){
  handleModalShow('用户档案信息详情');
}
// ------------------------------新增--------------------------------
function handleAdd(){
  handleModalShow('新增用户档案');
}
// ------------------------------修改--------------------------------
function handleEdit(){
  handleModalShow('修改用户档案');
}
// ------------------------------删除--------------------------------
function handleDelete(row){
  ElMessageBox.confirm(`确认删除档案名称为【${row.name}】的数据`,'提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteNow(row);
  }).catch(() => {})
}
async function deleteNow(row){
  const loading = ElLoading.service({
    lock: true,
    text: '删除中~',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  // deleteGasMeterInfoApi(row.id).then(()=>{
  //   loading.close();
  //   proxy.$modal.msgSuccess(row.name+'燃气表删除成功');
  //   getList();
  // }).catch(()=>{
  //   loading.close();
  // })
}
</script>
<style lang='scss' scoped>
@import "@/assets/styles/public.scss";
:deep(.el-input){
  .el-input__wrapper{
    background-color: $formBgSet;
    box-shadow: none;
    border-radius: 0 var(--el-border-radius-base) var(--el-border-radius-base) 0;
  }
}
:deep(.el-select){
  .el-select__wrapper{
    background-color: $formBgSet;
    box-shadow: none;
    border-radius: var(--el-border-radius-base) 0 0 var(--el-border-radius-base);
  }
}
:deep(.el-drawer){
  .el-drawer__header{
    margin: 0;
    padding-bottom: 20px;
  }
  .el-drawer__body{
    padding: 0;
  }
}

.app-container{
  width: 100%;
  min-height: calc(100vh - 66px);
  background: #F8F8F9;
  .contentBox{
    width: 100%;
    background-color: #FFFFFF;
    border-radius: 5px;
    box-sizing: border-box;
    padding: 12px;
    .searchBox{
      .selectOptionBox{
        width: 200px;
        &:deep(.valueBox){
          border-radius: var(--el-border-radius-base);
        }
      }
      .searchBtn{
        width: 32px;
        height: 32px;
        font-size: 12px;
        padding: 0;
        border: none;
        margin: 0 6px 0 0px;
      }
    }
    .btnSetBox{
      padding: 0 10px;
      box-sizing: border-box;
      border-right: 1px solid $borderColor;
      cursor: pointer;
      .text{
        height: 16px;
        line-height: 16px;
        color: var(--el-color-primary);
      }
      .img{
        width: 16px;
        height: 16px;
        margin-right: 2px;
      }
      &:last-child{
        border-right: none;
      }
    }
    .totalText{
      height: 22px;
      font-weight: 400;
      font-size: 14px;
      color: #1D2129;
      line-height: 22px;
    }
  }
}
</style>